package com.yanqu.road.dao.impl.cross.unionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.UnionWarUnionMember;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionWarUnionMemberDaoImpl extends TempDataObjectDao<UnionWarUnionMember> {

    public static String getUnionWarUnionMemberSql = "select * from t_u_activity_unionwar_union_member where activityId=?";
    public Map<String, Map<Integer, Map<Integer, UnionWarUnionMember>>> getUnionWarUnionMember(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Integer, Map<Integer, UnionWarUnionMember>>> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUnionWarUnionMemberSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionWarUnionMember data = parse(rs);
                if(!resultMap.containsKey(data.getUnionUid())){
                    resultMap.put(data.getUnionUid(), new ConcurrentHashMap<>());
                }
                if(!resultMap.get(data.getUnionUid()).containsKey(data.getPhase())){
                    resultMap.get(data.getUnionUid()).put(data.getPhase(), new ConcurrentHashMap<>());
                }
                resultMap.get(data.getUnionUid()).get(data.getPhase()).put(data.getSession(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }


    public static String insertUnionWarUnionMemberSql = "insert into t_u_activity_unionwar_union_member(activityId,unionUid,phase,session,unionBaseInfo,userIdList) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionWarUnionMember data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhase()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSession()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getUserIdList(), ",")));
        if (getDb().executeNoneQuery(insertUnionWarUnionMemberSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionWarUnionMemberSql = "update t_u_activity_unionwar_union_member set unionBaseInfo=?,userIdList=? where activityId=? and unionUid=? and phase=? and session=?";

    @Override
    protected boolean updateData(UnionWarUnionMember data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getUserIdList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhase()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSession()));
        if (getDb().executeNoneQuery(updateUnionWarUnionMemberSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionWarUnionMember parseData(ResultSet rs) throws SQLException {
        UnionWarUnionMember unionWarUnionMember = new UnionWarUnionMember();
        unionWarUnionMember.setActivityId(rs.getInt("activityId"));
        unionWarUnionMember.setUnionUid(rs.getString("unionUid"));
        unionWarUnionMember.setPhase(rs.getInt("phase"));
        unionWarUnionMember.setSession(rs.getInt("session"));

        String baseUserInfoStr = rs.getString("unionBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseUserInfoStr)){
            UnionBaseInfo unionBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UnionBaseInfo>() {});
            if(null != unionBaseInfo) {
                unionWarUnionMember.setUnionBaseInfo(unionBaseInfo);
            }
        }
        unionWarUnionMember.setUserIdList(StringUtils.stringToLongList(rs.getString("userIdList"), ","));

        return unionWarUnionMember;
    }
}
